﻿Imports BusinessObject
Imports DataObject
Imports System.ComponentModel
Imports System.Drawing.Printing
Imports System.Reflection

Public Class frmTaiLieuDinhKemDetail

    Public Enum EditMode As Integer
        Edit = 0
        AddNew = 1
        QuickAdd = 2
        Delete = 3
        None = 4
        [Select] = 5
    End Enum

    Public Property RowDetail() As DataRow
        Get
            Return oRow
        End Get
        Set(ByVal Value As DataRow)
            oRow = Value
        End Set
    End Property

    Public Property ActionMode() As EditMode
        Get
            Return eActionMode
        End Get
        Set(ByVal value As EditMode)
            eActionMode = value
        End Set
    End Property

    Public Property MaHopDong() As String
        Get
            Return sMaHopDong
        End Get
        Set(ByVal value As String)
            sMaHopDong = value
        End Set
    End Property

    Public Property TaiLieuDinhKemTable() As DataTable
        Get
            Return tblTaiLieuDinhKem
        End Get
        Set(ByVal value As DataTable)
            tblTaiLieuDinhKem = value
        End Set
    End Property

    Private tblTaiLieuDinhKem As DataTable
    Private sMaHopDong As String
    Protected oRow As DataRow
    Protected eActionMode As EditMode
    Protected fileDetail As IO.FileInfo
    Protected oFunction As New hDCommonFunction

#Region "Thangnd - dung delegate la cach thich hop cho viec get data tu nhieu form"
    Public Delegate Sub PassData(ByVal value As DataTable)
    Public m_passdata As PassData
#End Region 'End thangnd

    Private Sub frmTaiLieuDinhKemDetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If (ActionMode = EditMode.AddNew) Then
            Me.Text = "Thêm tài liệu đính kèm"
        Else
            Me.Text = "Sửa tài liệu đính kèm"
        End If
        LoadData()
    End Sub

    Private Sub LoadData()
        txtTenTaiLieu.Text = RowDetail.Item("TenTaiLieuDinhKem")
        txtTenTepTin.Text = RowDetail.Item("TenTepTin")
        txtDienGiai.Text = RowDetail.Item("DienGiai")
    End Sub

    Private Function ValidateData() As Boolean
        If txtTenTaiLieu.Text = "" Then
            MessageBox.Show("Tên tài liệu không được phép để trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtTenTaiLieu.Focus()
            Return False
        End If
        If txtTenTepTin.Text = "" Then
            MessageBox.Show("Bạn chưa chọn tài liệu đính kèm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
            btnChonTepTin.Focus()
            Return False
        End If
        Return True
    End Function

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        Dim row As DataRow
        Try
            If (eActionMode = EditMode.AddNew) Then
                row = TaiLieuDinhKemTable.NewRow
                With row
                    .Item("MaHopDong") = MaHopDong
                    .Item("TenTaiLieuDinhKem") = txtTenTaiLieu.Text
                    .Item("TenTepTin") = txtTenTepTin.Text
                    .Item("KichThuocTaiLieuDinhKem") = fileDetail.Length
                    .Item("DinhDangTepTin") = fileDetail.Extension.ToString()
                    .Item("DienGiai") = txtDienGiai.Text
                    .Item("NoiDungTepTin") = oFunction.ReadFiletoByte(dglOpenFile.FileName)
                End With
                ''dsQuanLyHD.TaiLieuDinhKem.Rows.Add(row)
                TaiLieuDinhKemTable.Rows.Add(row)
                m_passdata(TaiLieuDinhKemTable)
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub btnChonTepTin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChonTepTin.Click
        If (dglOpenFile.ShowDialog = Windows.Forms.DialogResult.OK) Then
            txtTenTepTin.Text = System.IO.Path.GetFileName(dglOpenFile.FileName)
            fileDetail = My.Computer.FileSystem.GetFileInfo(dglOpenFile.FileName)
        End If
    End Sub

    Private Sub btnHuyBo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuyBo.Click
        Me.Close()
    End Sub

    Protected Sub ShowFormDetail()
        Dim ofrm As frmHopDongMuaDetail
        ofrm = GetFormDetail()
        If Not IsNothing(ofrm) Then
            With ofrm
                .TableTaiLieuDinhKem = dsQuanLyHD.TaiLieuDinhKem
                .LoadDataGrid()
            End With
            Me.Close()
        End If
    End Sub

    Protected Function GetFormDetail() As Form
        Dim frm As frmHopDongMuaDetail 'Object
        Dim oFormType As Type
        oFormType = [Assembly].GetExecutingAssembly.GetType(frmMain.GetType.Namespace + "." + "frmHopDongMuaDetail")
        If Not IsNothing(oFormType) Then
            frm = Activator.CreateInstance(oFormType)
        Else
            frm = Nothing
        End If
        Return frm
    End Function

    Private Sub btnTaiTepTin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTaiTepTin.Click
        Process.Start(dglOpenFile.FileName)
    End Sub
End Class